package com.fyzk.activity.trend;

import android.util.Log;

import com.tyjc.lottery.R;
import com.fyzk.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import static com.tyjc.unique._AnalaysesBaseFragment.TAG;

/**
 * Created by fanxs11 on 2017/7/16.
 */

public class K3TrendBean extends TrendBean{
    @Override
    public List<List<String>> getMissValues(int payType, int chooseId, String missTitle, String hcTitle) {
        List<List<String>> missValues = new ArrayList<>();
        if (issues == null || issues.size() == 0 || misses == null || misses.size() == 0 || hcs == null || hcs.size() == 0) {
            return missValues;
        }

        switch (chooseId) {
            case R.id.trend_choose_rb1:     // 第一个选项卡——开奖
                for (int i = 0; i < issues.size(); i++) {
                    IssuesBean issuesBean = issues.get(i);
                    List<String> missNums = new ArrayList<>();
                    missNums.add(issuesBean.getWincode());  // 开奖号码
                    missNums.add(issuesBean.getAndValue());      // 和值
                    int andValueNum = 0;
                    try {
                        andValueNum = Integer.parseInt(issuesBean.getAndValue());
                    } catch (Exception e) {

                    }
                    String bs = "-";
                    if (andValueNum > 10) {
                        bs = "大";     // 大小
                    } else {
                        bs = "小";     // 大小
                    }
                    missNums.add(bs);     // 大小
                    missNums.add(issuesBean.getSingleDoubleValue());     // 单双
                    missValues.add(missNums);
                }
                return missValues;
            case R.id.trend_choose_rb2:     // 第二个选项卡——基本走势

                return getBaseTrend(missTitle);
            case R.id.trend_choose_rb3:
                switch (payType) {
                    case 0:     // 和值走势
                        return getZouShi(missTitle);
                    case 1:     // 形态走势
                    case 3:
                    case 5:
                    case 2:     // 号码分布
                    case 4:
                    case 6:
                        return getShapTrend(missTitle);
                }
            case R.id.trend_choose_rb4:
                HcsBean hcsBean30 = hcs.get(0);
                HcsBean hcsBean50 = hcs.get(1);
                HcsBean hcsBean100 = hcs.get(2);
                // 最新期遗漏
                List<String> zouShiMiss = new ArrayList<>();
                Log.d(TAG, "getMissValues: payType:"+payType);
                if (payType == 0) {
                    zouShiMiss = getOneZouShi(misses.size()-1,missTitle);
                } else {
                    List<Integer> missVs = new ArrayList<>();
                    BasicMissesBean missesBean = basicMisses.get(basicMisses.size()-1);
                    this.maxMiss[3] = missesBean.maxMiss+"";
                    missVs = missesBean.getMissValues(missTitle);
                    for (int j = 0; j < missVs.size(); j++) {
                        zouShiMiss.add(missVs.get(j)+"");
                    }
                }
                List<Integer> hcs30Misses = hcsBean30.getHcValues(hcTitle);
                List<Integer> hcs50Misses = hcsBean50.getHcValues(hcTitle);
                List<Integer> hcs100Misses = hcsBean100.getHcValues(hcTitle);
                for (int i = 0; i < hcs30Misses.size(); i++) {

                    List<String> missNums = new ArrayList<>();
                    try {
                        missNums.add(hcs30Misses.get(i)+"");    // 30期冷热
                        missNums.add(hcs50Misses.get(i)+"");    // 50期冷热
                        missNums.add(hcs100Misses.get(i)+"");   // 100期冷热
                    } catch (IndexOutOfBoundsException e) {
                        LogUtil.d(e.getMessage());
                    }

                    if (i < zouShiMiss.size()) {
                        missNums.add(zouShiMiss.get(i));
                    } else {
                        missNums.add("-");
                    }

                    missValues.add(missNums);
                }
                this.maxMiss[0] = hcsBean30.maxHc+"";
                this.maxMiss[1] = hcsBean50.maxHc+"";
                this.maxMiss[2] = hcsBean100.maxHc+"";
                return missValues;

            default:
                break;
        }

        return missValues;
    }

    private List<List<String>> getBaseTrend(String missTitle) {
        List<List<String>> missValues = new ArrayList<>();
        int rowCount = 0;
        if (basicMisses.size() > 0) {
            rowCount = basicMisses.get(0).getMissValues(missTitle).size();
        }
        int[] showCounts = new int[rowCount];
        int[] averageMiss = new int[rowCount];
        int[] maxMiss = new int[rowCount];
        int[] maxLianMiss = new int[rowCount];
        int[] lian = new int[rowCount];
        for (int x = 0; x < lian.length; x++) {
            lian[x] = 1;
        }

        for (int i = 0; i < basicMisses.size(); i++) {
            BasicMissesBean missesBean = basicMisses.get(i);
            List<String> missNums = new ArrayList<>();
            List<Integer> missVs = missesBean.getMissValues(missTitle);
            List<Integer> missVs2 = missVs;
            BasicMissesBean missesBean2;
            if (i > 0) {
                missesBean2 = basicMisses.get(i-1);
                missVs2 = missesBean2.getMissValues(missTitle);
            }

            missNums.add(issues.get(i).getWincode());
            missNums.add(issues.get(i).getAndValue());
            missNums.add(issues.get(i).getSpanValue());
            for (int j = 0; j < missVs.size(); j++) {
                missNums.add(missVs.get(j)+"");

                // 统计
                if (missVs.get(j) == 0) {
                    showCounts[j]++;
                }
                if (i > 0 && missVs.get(j) == 0) {
                    averageMiss[j] += missVs2.get(j);
                }
                if (i == misses.size()-1) {
                    averageMiss[j] += missVs.get(j);
                }
                if (missVs.get(j) > maxMiss[j]) {
                    maxMiss[j] = missVs.get(j);
                }
                if (i > 0) {
                    if (missVs2.get(j) == missVs.get(j)) {
                        lian[j]++;
                    } else {
                        lian[j] = 1;
                    }
                    if (maxLianMiss[j] < lian[j]) {
                        maxLianMiss[j] = lian[j];
                    }
                }
            }

            missValues.add(missNums);
        }

        List<String> showCounts_list = new ArrayList<>();
        List<String> averageMiss_list = new ArrayList<>();
        List<String> maxMiss_list = new ArrayList<>();
        List<String> maxLianMiss_list = new ArrayList<>();

        for (int i = -3; i < showCounts.length; i++) {
            if (i >= 0) {
                showCounts_list.add(showCounts[i]+"");
                int average;
                if (showCounts[i] == 0) {
                    average = maxMiss[i];
                } else {
                    average = averageMiss[i]/(showCounts[i]+1);
                    if (average == 0) {
                        average = 1;
                    }
                }
                averageMiss_list.add(average+"");
                maxMiss_list.add(maxMiss[i]+"");
                if (showCounts[i] == 0) {
                    maxLianMiss[i] = 0;
                }
                maxLianMiss_list.add(maxLianMiss[i]+"");
            } else {
                showCounts_list.add("");
                averageMiss_list.add("");
                maxMiss_list.add("");
                maxLianMiss_list.add("");
            }
        }

        missValues.add(showCounts_list);	// 出现次数
        missValues.add(averageMiss_list);	// 平均遗漏
        missValues.add(maxMiss_list);		// 最大遗漏
        missValues.add(maxLianMiss_list);	// 最大连出
        return missValues;
    }

    // 获取形态走势
    private List<List<String>> getShapTrend(String missTitle) {
        List<List<String>> missValues = new ArrayList<>();
        int rowCount = 0;
        if (misses.size() > 0) {
            rowCount = misses.get(0).getMissValues(missTitle).size();
        }
        int[] showCounts = new int[rowCount];
        int[] averageMiss = new int[rowCount];
        int[] maxMiss = new int[rowCount];
        int[] maxLianMiss = new int[rowCount];
        int[] lian = new int[rowCount];
        for (int x = 0; x < lian.length; x++) {
            lian[x] = 1;
        }

        for (int i = 0; i < misses.size(); i++) {
            MissesBean missesBean = misses.get(i);
            List<String> missNums = new ArrayList<>();
            List<Integer> missVs = missesBean.getMissValues(missTitle);
            List<Integer> missVs2 = missVs;
            MissesBean missesBean2;
            if (i > 0) {
                missesBean2 = misses.get(i-1);
                missVs2 = missesBean2.getMissValues(missTitle);
            }

            missNums.add(issues.get(i).getWincode());

            for (int j = 0; j < missVs.size(); j++) {
                missNums.add(missVs.get(j)+"");

                // 统计
                if (missVs.get(j) == 0) {
                    showCounts[j]++;
                }
                if (i > 0 && missVs.get(j) == 0) {
                    averageMiss[j] += missVs2.get(j);
                }
                if (i == misses.size()-1) {
                    averageMiss[j] += missVs.get(j);
                }
                if (missVs.get(j) > maxMiss[j]) {
                    maxMiss[j] = missVs.get(j);
                }
                if (i > 0) {
                    if (missVs2.get(j) == missVs.get(j)) {
                        lian[j]++;
                    } else {
                        lian[j] = 1;
                    }
                    if (maxLianMiss[j] < lian[j]) {
                        maxLianMiss[j] = lian[j];
                    }
                }
            }

            missValues.add(missNums);
        }

        List<String> showCounts_list = new ArrayList<>();
        List<String> averageMiss_list = new ArrayList<>();
        List<String> maxMiss_list = new ArrayList<>();
        List<String> maxLianMiss_list = new ArrayList<>();

        for (int i = -1; i < showCounts.length; i++) {
            if (i >= 0) {
                showCounts_list.add(showCounts[i]+"");
                int average;
                if (showCounts[i] == 0) {
                    average = maxMiss[i];
                } else {
                    average = averageMiss[i]/(showCounts[i]+1);
                    if (average == 0) {
                        average = 1;
                    }
                }

                averageMiss_list.add(average+"");
                maxMiss_list.add(maxMiss[i]+"");
                if (showCounts[i] == 0) {
                    maxLianMiss[i] = 0;
                }
                maxLianMiss_list.add(maxLianMiss[i]+"");
            } else {
                showCounts_list.add("");
                averageMiss_list.add("");
                maxMiss_list.add("");
                maxLianMiss_list.add("");
            }
        }

        missValues.add(showCounts_list);	// 出现次数
        missValues.add(averageMiss_list);	// 平均遗漏
        missValues.add(maxMiss_list);		// 最大遗漏
        missValues.add(maxLianMiss_list);	// 最大连出
        return missValues;
    }

    protected List<String> getOneZouShi(List<Integer> missVs) {
        List<String> missNums = new ArrayList<>();

        for (int j = 0; j < missVs.size(); j++) {
            missNums.add(missVs.get(j)+"");

        }
        return missNums;
    }
}
